import 'package:flutter/material.dart';


void main() {
  runApp(const MyApp());
}


class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context)  => MaterialApp(
    theme: ThemeData(
      primaryColor: Colors.lightBlue[800],//主题色为蓝色
    ),
    home: const MyHomePage(title: 'Custom UI', key: Key('1'),),
  );

}

class MyHomePage extends StatelessWidget {
  const MyHomePage({required Key key, required this.title}) : super(key: key);
  final String title;

  @override
  Widget build(BuildContext context) {
    return const DefaultTabController(
      length: 3,
      child: Scaffold(
        body: TabBarView(
          children: [
            ParallelWidget(),
            ScrollNotificationWidget(),
            ScrollControllerWidget(),
          ],
        ),
        bottomNavigationBar: TabBar(
          tabs: [
            Tab(icon: Icon(Icons.home),text: "视差",),
            Tab(icon: Icon(Icons.rss_feed),text: "Notification",),
            Tab(icon: Icon(Icons.perm_identity),text: "Controller",)
          ],
          unselectedLabelColor: Colors.blueGrey,
          labelColor: Colors.blue,
          indicatorSize: TabBarIndicatorSize.label,
          indicatorColor: Colors.red,
        ),
      ),
    );
  }
}


class ParallelWidget extends StatelessWidget {
  const ParallelWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return
      CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(//SliverAppBar 作为头图控件
              title: const Text('CustomScrollView Demo'),// 标题
              floating: true,// 设置悬浮样式
              flexibleSpace: Image.network("https://file.40017.cn/baoxian/health/health_public/images/banner_bg.png",fit:BoxFit.cover),// 设置悬浮头图背景
              expandedHeight: 130,// 头图控件高度
            ),
            SliverList(//SliverList 作为列表控件
              delegate: SliverChildBuilderDelegate(
                    (context, index) => ListTile(title: Text('Item #$index')),// 列表项创建方法
                childCount: 100,// 列表元素个数
              ),
            ),
          ]);
  }

}



class ScrollNotificationWidget extends StatelessWidget {
  const ScrollNotificationWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Scroll Notification Demo',
        home: Scaffold(
            appBar: AppBar(title: const Text('ScrollController Demo')),
            body: NotificationListener<ScrollNotification>(// 添加 NotificationListener 作为父容器
              onNotification: (scrollNotification) {// 注册通知回调
                if (scrollNotification is ScrollStartNotification) {// 滚动开始
                  print('Scroll Start');
                } else if (scrollNotification is ScrollUpdateNotification) {// 滚动位置更新
                  print('Scroll Update');
                } else if (scrollNotification is ScrollEndNotification) {// 滚动结束
                  print('Scroll End');
                }
                return true;
              },
              child: ListView.builder(
                itemCount: 30,// 列表元素个数
                itemBuilder: (context, index) => ListTile(title: Text("Index : $index")),// 列表项创建方法
              ),
            )
        )
    );
  }
}

class ScrollControllerWidget extends StatefulWidget {
  const ScrollControllerWidget({super.key});

  @override
  State<StatefulWidget> createState()=> _ScrollControllerState();
}

class _ScrollControllerState extends State<ScrollControllerWidget> {
  late ScrollController _controller;//ListView 控制器
  bool isToTop = false;// 标示目前是否需要启用 "Top" 按钮
  @override
  void initState() {
    _controller = ScrollController();
    _controller.addListener(() {// 为控制器注册滚动监听方法
      if(_controller.offset > 1000) {// 如果 ListView 已经向下滚动了 1000，则启用 Top 按钮
        setState(() {isToTop = true;});
      } else if(_controller.offset < 300) {// 如果 ListView 向下滚动距离不足 300，则禁用 Top 按钮
        setState(() {isToTop = false;});
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Scroll Controller Widget")),
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 40.0,
            child: FilledButton(onPressed: (isToTop ? () {
              if (isToTop) {
                _controller.animateTo(.0,
                    duration: const Duration(milliseconds: 200),
                    curve: Curves.ease
                ); // 做一个滚动到顶部的动画
              }
            } : null), child: const Text("Top"),),
          ),
          Expanded(
            child: ListView.builder(
              controller: _controller, // 初始化传入控制器
              itemCount: 100, // 列表元素总数
              itemBuilder: (context, index) =>
                  ListTile(title: Text("Index : $index")), // 列表项构造方法
            ),
          ),
        ],
      ),
    );
  }
  @override
  void dispose() {
    _controller.dispose(); // 销毁控制器
    super.dispose();
  }
}